home *** CD-ROM | disk | FTP | other *** search
- Path: engnews2.Eng.Sun.COM!usenet
- From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
- Newsgroups: comp.lang.c++
- Subject: Re: Help: How to initialize objects created by new class[x]
- Date: 05 Jan 1996 21:08:53 GMT
- Organization: SunSoft
- Message-ID: <NITIN.96Jan5130853@more.eng.sun.com>
- References: <4cicd6$lsv@news.capitalnet.com>
- <30ECFA47.446B9B3D@intellektik.informatik.th-darmstadt.de>
- NNTP-Posting-Host: more.eng.sun.com
- In-reply-to: Enno Sandner's message of Fri, 05 Jan 1996 11:15:35 +0100
-
- In article <30ECFA47.446B9B3D@intellektik.informatik.th-darmstadt.de> Enno Sandner <enno@intellektik.informatik.th-darmstadt.de> writes:
-
- $ From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
- $ Newsgroups: comp.lang.c++
- $ Date: Fri, 05 Jan 1996 11:15:35 +0100
- $ Organization: Fachbereich Informatik, TH Darmstadt
-
- $ wingl@capitalnet.com wrote:
- $ >
- $ > Let say I have the following class
- $ >
- $ > My_class {
- $ > public:
- $ > int (int i=0) : a(i), b(i) {}
- $ > private:
- $ > int a, b;
- $ > }
- $ >
- $ > For the following statement, default constructor is involved upon
- $ > the creation of the object.
- $ >
- $ > My_class* my_ptr= new My_class;
- $ >
- $ > However, if I use
- $ > My_class* my_ptr= new My_class[10];
- $ >
- $ > to allocate 10 objects of class My_class, then C++ would not
- $ > call any constructor to do any initialization. All the objects
- $ > allocated are not initializated at all.
- $ >
- $ > I wonder what is the proper way to perform object initialization
- $ > whenever an array of objects are created? As long as a constructor
- $ > is involved for each of the object in the array, I don't care if all the
- $ > objects are initializated the same or not.
- $ >
- $ > If there is no way to get constructor to initialize an array of objects
- $ > upon creation. Does that means one should not do so?
- $ >
-
- $ That's not true. The expression 'new My_class[10]' should call the
- $ default-ctor of 'My_class' for each element.
- $ (I will assume the class looks like
- $
- $ class My_class {
- $ public:
- $ My_class (int i=0) : a(i), b(i) {}
- $ private:
- $ int a, b;
- $ };
- $
- $ ).
- $ The problems start when your class doesn't provide a default-ctor
- $ or you want to initialize elements of the array with different values.
- $ Here you are out of luck, because C++ doesn't provide an appropriate
- $ syntax. You'll have to use some hack like using the placement-new/delete
- $ operators.
-
- $ Enno
-
- You can define your class as follows:
-
- class My_class {
- public:
- My_class () { init(); }
- My_class (int i) { init(i); }
- private:
- int a, b;
- void init(int i=0) { a = i; b = i; }
- };
-
- In other words,
- (1) define an init method.
- (2) define a default constructor to call init.
- (3) Remove default argument from your original constructor so that the compiler
- can distinguish between the default and the other constructor.
- (4) Call the new init method from your original constructor.
-
- -Nitin
- --
- ----------------------------------------------------------------------
- Nitin More
- SunSoft, Bldg 16 Off: (415) 786 7109
- Menlo Park, CA Fax: (415) 786 7957 e-mail: nitin@more.eng.sun.com
- ----------------------------------------------------------------------
-